////销售政策 新建 
//存在有三种 政策模型:
//1.普通政策
//2.配套政策 : 仪器存在 配套仪器
//3.组合赠送政策: 存在某几种仪器的组合时,选择是否要赠送仪器
public without sharing class InstrumentPolicyController {
    public String backId {get;private  set;}//业务机会Id 

    public String msg {get; private set;}//信息
    public Boolean isErr {get; private set;}//错误
    public Boolean isSuccess {get; private set;}//是否保存成功
    public InstrumentPolicy__c ip {get; private set;}

    public List<PolicyBean> pList {get; set;}
    public String  dataJson {get ;private set;}
    public String  ptdataJson {get ;private set;}
    public String saveJson {get ; set;}
    public String searchJson {get; set;}

    public String typecombox {get; private set;}

    public Boolean isEidtable {get ;private set;}
    public String systemset ;//第三方体系

    public Boolean isDealer {get; private set; }

    public String cate1combox {get; private set;}
    public String cate2combox {get; private set;}
    public String cate3combox {get; private set;}
    public String freeProductJson {get; private set;}
    public String freeProductSetJson  {get; private set;}

    private Boolean isNew ;

    public Boolean isSaler {get ; private set;}

    public Boolean isSelectProduct {get; private set;}

    public Boolean isGPO {get; private set;}//是否为gpo 业务

    private User u;



    public class ControllerException extends Exception {}

    public InstrumentPolicyController() {

         backId = System.currentPageReference().getParameters().get('id');

         isGPO = System.currentPageReference().getParameters().get('gpo') != null;
         ip = new InstrumentPolicy__c ();
         pList = new List<PolicyBean>();
        
         typecombox = getOptMap(ImportObject.getPicklistValues('InstrumentMarketingPolicy__c','PolicyType__c',false));
        
         isEidtable = true;
         
         isNew = false;

        u =  [select Profile.Name  from User where Id =:UserInfo.getUserId()];

         isSaler = (u.Profile.Name =='销售部分销销售员'  || u.Profile.Name=='销售部直销销售员');
        // isSaler = true;
       // if(isGPO && !isSaler )isSaler = u.Profile.Name != '销售部分销经理/主管' ||  !u.Profile.Name.contains('销售部直销经理/主管'); 


    }
    private String getOptsFromLabel(String lb){
        Map<String,String> tmpMap = new Map<String,String>();

        tmpMap.put('', System.Label.Option_None);
        for (String str : lb.split(',', -1)) {
            tmpMap.put(str, str);
        }
        return getOptMap(tmpMap);
    }

    public String getOptMap(Map<String,String> opts){
       Map<String ,Object> cooMap = new Map<String ,Object> ();
      cooMap.put('valueField', 'value');
      cooMap.put('textField', 'label');
      cooMap.put('panelHeight', '200');
      cooMap.put('limitToList', 'true');
      List<Map<String,String>>  dList = new List<Map<String,String>>();
      for (String k : opts.keySet()) {
        Map<String ,String> dataMap = new Map<String,String>();
        dataMap.put('label',opts.get(k) );
        dataMap.put('value',k );
        dList.add(dataMap);
      }  
      cooMap.put('data', dList);   
      
      return EncodingUtil.urlEncode(JSON.serialize(cooMap), 'UTF-8');
    }

    public void setNil(){
        dataJson = '[]';
        ptdataJson = '[]';
        freeProductJson = '[]';
        freeProductSetJson = '{}';
    }
    public void init(){

        try {
            if(backId == null) setErr('页面打开错误!');
           // 客户记录类型为经销商的或者客户上第三方
            if(Id.valueOf(backId).getSobjectType() == Opportunity.SObjectType){//新建

                isNew = true ;
                //ExecutiveCompany__r.System__c,ExecutiveCompany__c ,ExecutiveCompany__r.RecordType.DeveloperName

                Opportunity opp = [select RecordType.DeveloperName,Account.AccountNumber ,StageName ,Account.RecordType.DeveloperName,Account.Parent.AccountNumber , AccountId,Account.System__c, Owner.SubCompany__c  
                from Opportunity where Id=:backId];
                if(opp.StageName == '合同签订' || opp.StageName == '失败' )setErr('业务机会'+opp.StageName +'阶段不能操作!');
                if(isGPO && opp.Account.AccountNumber != '301133' && opp.Account.Parent.AccountNumber != '301133') setErr('非GPO体系不能新建GPO政策!');
                if(opp.Account.RecordType.DeveloperName  != 'Dealer' &&  opp.Account.System__c == null ) setErr('业务机会客户为经销商的或者第三方才能新建销售政策');
                ip.Company__c = ImportObject.getPicklistValues('InstrumentPolicy__c','Company__c',false).get(opp.Owner.SubCompany__c);
                ip.Opportunity__c  = backId;
                
                ip.Account__c = opp.AccountId;// opp.ExecutiveCompany__r.RecordType.DeveloperName =='Maccura' ? opp.AccountId : opp.ExecutiveCompany__c ;//开票客户
                ip.RecordTypeId = isGPO ? Utils.getRecordTypeIdByDevNameAndObjName('GPOInstrumentPolicy', 'InstrumentPolicy__c')  : Utils.getRecordTypeIdByDevNameAndObjName('InstrumentPolicy', 'InstrumentPolicy__c') ;
                //ip.TerminalCustomer__c = opp.AccountId;//终端客户
               
                if(opp.Account.System__c != null )systemset= opp.Account.System__c;
                //if(opp.ExecutiveCompany__r.System__c != null )systemset = opp.ExecutiveCompany__r.System__c;
                isDealer = (opp.RecordType.DeveloperName == 'DealerOpportunity') ? true : false;
                isSelectProduct = true;
                ip.Status__c = '草案中';


            }else if(Id.valueOf(backId).getSobjectType() == InstrumentPolicy__c.SObjectType){//编辑
                //TerminalCustomer__c
                //, TerminalCustomer__r.System__c
                 ip = [select RecordType.DeveloperName ,Id ,Company__c ,Status__c , Opportunity__c , Account__c ,LessThanTaskQuantityNumber__c,Opportunity__r.RecordType.DeveloperName ,
                        TYEquipment__c,MYEquipment__c,SHEquipment__c,BLEquipment__c,XXEquipment__c,XYEquipment__c ,Account__r.System__c
                    from InstrumentPolicy__c where Id =:backId  ];

                isGPO = ip.RecordType.DeveloperName ==  'GPOInstrumentPolicy';
                //if(isGPO && !isSaler )isSaler = u.Profile.Name != '销售部分销经理/主管' ||  !u.Profile.Name.contains('销售部直销经理/主管'); 

                if(isSaler && (ip.Status__c == '审批中' || ip.Status__c == '审批中(销售主管已批准)' )) throw new DmlException('审批中不能查看!');
                if(ip.Account__r.System__c != null )systemset=ip.Account__r.System__c;

               // if(ip.TerminalCustomer__r.System__c != null )systemset=ip.TerminalCustomer__r.System__c;

                //getDetails();
                isDealer = (ip.Opportunity__r.RecordType.DeveloperName == 'DealerOpportunity') ? true : false;
                isSelectProduct  = (ip.Status__c == '草案中' || ip.Status__c == '已拒绝' );
                isEidtable = isSelectProduct ? true : (!(ip.Status__c == '已批准' || ip.Status__c =='审批中(销售主管已批准)' ) &&  !isSaler);   


                if(ip.Status__c == '已批准')isEidtable = false ; 
            }else{

                setErr('页面打开错误!');

            }

            //isDealer = true ;
            if(isSelectProduct)getFreeEquipment();
            cate1combox = getOptsFromLabel(Label.Product_Category1);
            cate2combox = getOptsFromLabel(Label.Product_Category2);
            cate3combox = getOptsFromLabel(Label.Product_Category3);

        }catch (Exception e) {
            System.debug('InstrumentPolicyController==>'+ e);
            isErr = true ;
            ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()));
        }
        
    }
    private void setErr(String message){
        isErr  =true;
        throw new ControllerException(message);
    }
    ///获取 明细数据
    public void getDetails(){

        if(Id.valueOf(backId).getSobjectType() == InstrumentPolicy__c.SObjectType){
            String sql = 'select Id,IsFree__c,EarnestMoney__c,CooperationPeriod__c,StartDate__c,SupportAmount__c,MarketingPolicy__r.BloodCardPurchased__c, IsRelated__c,'+
              '  Product__c , Product__r.Name  ,Product__r.Spec__c,Product__r.NCCode__c,Note__c, '+
               ' EndDate__c,Quantity__c,LeasingPolicyTerms__c,PolicyType__c,ProductBrand__c,ProductLine__c, '+
                'PresentingReagent__c,RewardCoefficient__c,RelatedType__c,'+
               'AmountPurchased__c,AmountPurchased2__c,AmountPurchased3__c,AmountPurchased4__c,AmountPurchased5__c,AmountPurchased6__c,'+
               'TaskIndicatorRebate1__c,TaskIndicatorRebate2__c,TaskIndicatorRebate3__c,TaskIndicatorRebate4__c,TaskIndicatorRebate5__c,TaskIndicatorRebate6__c,'+
                ' RentalFee__c,MaintenanceCost__c,Category2__c,ReturnServiceFee__c,SpecificItems__c, '+
                ' ReturnServiceFeeRentalFee__c,SalePrice__c,MarketingPolicy__c,BloodCardNumber__c,BloodCardPrice__c, '+
                ' MarketingPolicy__r.RentalFee__c,MarketingPolicy__r.EarnestMoney__c,MarketingPolicy__r.Product__c, '+
                ' MarketingPolicy__r.CooperationPeriod__c, '+
                ' MarketingPolicy__r.AmountPurchased__c,'+
                ' MarketingPolicy__r.AmountPurchased2__c,'+
                ' MarketingPolicy__r.AmountPurchased3__c,'+
                ' MarketingPolicy__r.AmountPurchased4__c,'+
                ' MarketingPolicy__r.AmountPurchased5__c,'+
                ' MarketingPolicy__r.AmountPurchased6__c,'+
                ' MarketingPolicy__r.TaskIndicatorRebate1__c, '+
                ' MarketingPolicy__r.TaskIndicatorRebate2__c, '+
                ' MarketingPolicy__r.TaskIndicatorRebate3__c, '+
                ' MarketingPolicy__r.TaskIndicatorRebate4__c, '+
                ' MarketingPolicy__r.TaskIndicatorRebate5__c, '+
                ' MarketingPolicy__r.TaskIndicatorRebate6__c, '+
                ' MarketingPolicy__r.WhetherIncludedTax__c,'+
                ' MarketingPolicy__r.PresentingReagent__c,'+
                ' MarketingPolicy__r.RewardCoefficient__c,'+

                'TerminalAccunt__r.Name,TerminalAccunt__c,'+
                ' MarketingPolicy__r.MaintenanceCost__c,MarketingPolicy__r.ReturnServiceFee__c,' +
                ' MarketingPolicy__r.ReturnServiceFeeRentalFee__c,MarketingPolicy__r.SalePrice__c, '+
                ' MarketingPolicy__r.Product__r.Spec__c,MarketingPolicy__r.Product__r.SerialNo__c, FreeGift__c,'+
                ' MarketingPolicy__r.Product__r.NCCode__c,MarketingPolicy__r.Product__r.Model__c ,MarketingPolicy__r.MarketingPolicy__c,'+
                ' MarketingPolicy__r.Product__r.Name , MarketingPolicy__r.Dealer__c ,MarketingPolicy__r.LeasingPolicyTerms__c '+
                 ' from InstrumentMarketingPolicy__c where InstrumentPolicy__c =\''+backId+'\'  ';
            Map<String,Object> mp = new Map<String,Object>();
            List<PolicyBean> tmp = new List<PolicyBean>();
            List<PolicyBean> tmp1 = new List<PolicyBean>();
            List<PolicyBean> tmp2 = new List<PolicyBean>();
            //(List<InstrumentMarketingPolicy__c>)(isSaler?  Database.query(sql):
            List<InstrumentMarketingPolicy__c> ipList =  (!isEidtable) ? Database.query(sql) :  ( isSaler ? (List<InstrumentMarketingPolicy__c>) SQLUtil.query(sql):   Database.query(sql)) ; 
            Set<String>psets = new Set<String>();
            Set<String>policySet = new Set<String>();
            for (InstrumentMarketingPolicy__c imp :ipList ) {
                if(imp.IsRelated__c){
                     tmp2.add(new PolicyBean(imp,systemset));
                     continue;
                }
                psets.add(imp.Product__c );
                policySet.add(imp.MarketingPolicy__c);
                if(imp.IsFree__c)  tmp1.add(new PolicyBean(imp,systemset));
                if(!imp.IsFree__c)  tmp.add(new PolicyBean(imp,systemset));


            }
            Map<String, List<PolicyBean>> relatedMap =getRelatedProudctMap(psets,policySet);

            mp.put('policy', tmp);
            mp.put('free', tmp1 );
            mp.put('related', tmp2);
            mp.put('relatedMap', relatedMap );

            dataJson = EncodingUtil.urlEncode(JSON.serialize(mp), 'UTF-8');
        }

    }

    //获取 可供选择的 c800 is1200 i3000 可赠送 仪器
    public  void getFreeEquipment(){
        Map<String,Integer>  d  = new Map<String,Integer>();
        d.put('100010100001', 0);
        d.put('100020100001,100020100003', 0);
        //d.put('100020100003', 0);

        freeProductSetJson =  EncodingUtil.urlEncode(JSON.serialize(d), 'UTF-8');
        Set<String> pset = new Set<String>{'100030200004','100030200001','100030300002','100090100002','100090100001'};
            List<PolicyBean> tmp = new List<PolicyBean>();
        for(Product__c p :[select Id ,Name ,ProductName__c  ,Brand__c , Spec__c,Category0__c,Category1__c,Category2__c, Model__c, NCCode__c 
            from  Product__c where NCCode__c in: pset]){
            tmp.add(new PolicyBean(p,false));
        }
        freeProductJson = EncodingUtil.urlEncode(JSON.serialize(tmp), 'UTF-8');
    }

    public void save (){

        List<Object> resList = (List<Object>) JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));
        if(resList.size() == 0){
            msg = '没有数据!';
            return ; 
        }

        List<InstrumentMarketingPolicy__c> impList = new List<InstrumentMarketingPolicy__c>();
        /*Integer ty = 0; 
        Integer my = 0; 
        Integer sh = 0; 
        Integer bl = 0; 
        Integer xx = 0; 
        Integer xy = 0; */
        for (Object o  : resList) {
            Map<String,Object> mo = (Map<String,Object>) o ; 
            InstrumentMarketingPolicy__c imp = new InstrumentMarketingPolicy__c();
            imp.Quantity__c = getDecimalValue(mo.get('eqNum'));
            imp.MaintenanceCost__c = getDecimalValue(mo.get('maintenanceFee'));
            imp.ReturnServiceFeeRentalFee__c = getDecimalValue(mo.get('returnServiceAndRentFeeStand'));
            imp.ReturnServiceFee__c = getDecimalValue(mo.get('returnServiceFeeStand'));
            imp.RentalFee__c = getDecimalValue(mo.get('rentFee'));
            imp.MarketingPolicy__c = String.valueOf(mo.get('mpId'));

            imp.CooperationPeriod__c = getDecimalValue(mo.get('coopYear'));
            Decimal coopYear = imp.CooperationPeriod__c == null ? 0 : imp.CooperationPeriod__c;
            if(coopYear >= 0)imp.AmountPurchased__c = getDecimalValue(mo.get('amountPerYear0'));
            if(coopYear > 1)imp.AmountPurchased2__c = getDecimalValue(mo.get('amountPerYear1'));
            if(coopYear > 2)imp.AmountPurchased3__c = getDecimalValue(mo.get('amountPerYear2'));
            if(coopYear > 3)imp.AmountPurchased4__c = getDecimalValue(mo.get('amountPerYear3'));
            if(coopYear > 4)imp.AmountPurchased5__c = getDecimalValue(mo.get('amountPerYear4'));
            if(coopYear > 5)imp.AmountPurchased6__c = getDecimalValue(mo.get('amountPerYear5'));

            if(coopYear >= 0)imp.TaskIndicatorRebate1__c = getDecimalValue(mo.get('taskIndicator0'));
            if(coopYear > 1)imp.TaskIndicatorRebate2__c = getDecimalValue(mo.get('taskIndicator1'));
            if(coopYear > 2)imp.TaskIndicatorRebate3__c = getDecimalValue(mo.get('taskIndicator2'));
            if(coopYear > 3)imp.TaskIndicatorRebate4__c = getDecimalValue(mo.get('taskIndicator3'));
            if(coopYear > 4)imp.TaskIndicatorRebate5__c = getDecimalValue(mo.get('taskIndicator4'));
            if(coopYear > 5)imp.TaskIndicatorRebate6__c = getDecimalValue(mo.get('taskIndicator5'));

            if(mo.get('relatedType') != null )imp.RelatedType__c = String.valueOf(mo.get('relatedType'));

            imp.PresentingReagent__c = getDecimalValue(mo.get('presentRegent'));
            imp.RewardCoefficient__c = getDecimalValue(mo.get('rewardCoefficient'));

            imp.EarnestMoney__c = getDecimalValue(mo.get('entrueMoney'));
            imp.SalePrice__c = getDecimalValue(mo.get('salesprice'));
            imp.Category2__c =  String.valueOf(mo.get('cate2'));
            imp.PolicyType__c = String.valueOf(mo.get('type'));
            imp.WhetherIncludedTax__c = String.valueOf(mo.get('tax'));
            imp.SupportAmount__c = getDecimalValue(mo.get('supportAmount'));
            imp.BloodCardPrice__c = getDecimalValue(mo.get('bloodPrice'));
            imp.BloodCardNumber__c = getDecimalValue(mo.get('bloodNo'));
            imp.ProductLine__c = String.valueOf(mo.get('cate1'));
            imp.ProductBrand__c = String.valueOf(mo.get('brand'));
            imp.SpecificItems__c = String.valueOf(mo.get('specificItems'));
            imp.IsFree__c  = Boolean.valueOf( String.valueOf(mo.get('isfree')));
            imp.IsRelated__c  = mo.get('isRelated') == null ? false : Boolean.valueOf( String.valueOf(mo.get('isRelated')));
            imp.Product__c   =  String.valueOf(mo.get('productId'));
           if( mo.get('terimalId') != null ) imp.TerminalAccunt__c  = mo.get('terimalId') == null ?null  : String.valueOf(mo.get('terimalId'));
            imp.Note__c = String.valueOf(mo.get('note'));
            if( mo.get('impId') != null && !isSelectProduct )imp.Id = String.valueOf(mo.get('impId'));
            imp.FreeGift__c = Boolean.valueOf(mo.get('isgift'));
            /*if(mo.get('cate1') == '体液')  ty ++;
            if(mo.get('cate1') == '免疫') my ++;
            if(mo.get('cate1') == '生化') sh ++;
            if(mo.get('cate1') == '病理') bl++;
            if(mo.get('cate1') == '血型') xx++;
            if(mo.get('cate1') == '血液') xy++;
            
            if(imp.AmountPurchased__c < getDecimalValue(mo.get('s_amountPerYear'))){
                ip.LessThanTaskQuantityNumber__c = ip.LessThanTaskQuantityNumber__c == null ? 1:  ip.LessThanTaskQuantityNumber__c +1;

            }*/
            impList.add(imp);
        }
      /* ip.TYEquipment__c = ty; 
        ip.MYEquipment__c = my; 
        ip.SHEquipment__c = sh; 
        ip.BLEquipment__c = bl; 
        ip.XXEquipment__c = xx; 
        ip.XYEquipment__c = xy; */

        SavePoint sp = Database.setSavepoint();
        try {

            if(ip.Id != null && ( ip.Status__c != '审批中' && ip.Status__c != '审批中(销售主管已批准)' && ip.Status__c!= '已批准')  )delete [select Id from InstrumentMarketingPolicy__c where InstrumentPolicy__c =:ip.Id ];
            System.debug('InstrumentPolicyController==>'+ip);
            if(ip.Status__c == '审批中' ) ip.Confirm__c = true;
            upsert ip;
            backId = ip.Id;
            for ( InstrumentMarketingPolicy__c imp : impList) {
                imp.InstrumentPolicy__c = ip.Id ;
            }
            upsert impList;
            backId = ip.Id ; 
            msg = '保存成功!';
            isSuccess = true;
        }catch (Exception e) {
            Database.rollback(sp);
            if(isNew)ip.Id= null; 
            msg = '保存失败!'+e.getMessage()+e.getStackTraceString();

            isSuccess = false;
        }   
        



    }
    private Decimal getDecimalValue(Object val){
        if(val == null)return null;
        String  d = String.valueOf(val).trim();
        if(d.length() == 0 || d == 'null')return null;
        return Decimal.valueOf(d);
    }

   
    ///组装{ 意向产品: [ 政策: [配套政策]]}
    public void getIntentProduct (){

        dataJson = '[]';
        List<PolicyBean> tmp = new List<PolicyBean>();
        List<MarketingPolicy__c> mtmp = new List<MarketingPolicy__c>();

        System.debug('InstrumentPolicyController==>'+systemset);

        //////////////////////
        //查询意向产品中的仪器
            //<产品Id,产品可选的政策>
            Map<Id, List<PolicyBean>> pbMap = new Map<Id,List<PolicyBean>>();
            //<产品Id,产品可选的gpo类型>
           // Map<Id,Set<String>>proGPOMap = new Map<Id,Set<String>>();

            List<String> pLi  = new List<String>(); 
            
           for(IntentProduct__c ip :  [select Product__c,Product__r.ProductName__c,Product__r.Name  ,Product__r.Brand__c,TerminalClient__r.Name,TerminalClient__c,

            TerminalClient__r.GPOHospital__c,
            Product__r.Spec__c ,Quantity__c, Product__r.Model__c,Product__r.NCCode__c,Product__r.Category1__c,Product__r.Category2__c
            from IntentProduct__c where Opportunity__c =:ip.Opportunity__c and Product__r.Category0__c = '仪器' ]){
                ///处理gpo 
                // if(isGPO && ip.TerminalClient__r.GPOHospital__c == null ) continue;
                
                // Set<String> gpoSet = proGPOMap.containsKey(ip.Product__c ) ? proGPOMap.get(ip.Product__c) : new Set<String>();
                // if(ip.TerminalClient__r.GPOHospital__c != null)gpoSet.add(ip.TerminalClient__r.GPOHospital__c  );
                // if(!gpoSet.isEmpty())proGPOMap.put(ip.Product__c , gpoSet);

                //标准政策
                pLi.add(ip.Product__c);
               // tmp.add(new PolicyBean(ip ,systemset ) );
               List<PolicyBean> t = null ; 
               t = pbMap.containsKey(ip.Product__c) ? pbMap.get(ip.Product__c )  : new List<PolicyBean>() ;
               t.add(new PolicyBean(ip ,systemset ) );
               pbMap.put(ip.Product__c , t);
           }

        //查询仪器中的政策  
        String sql = ' select Id ,PolicyType__c, EarnestMoney__c , CooperationPeriod__c , BloodCardPurchased__c,Dealer__c, PresentingReagent__c,RewardCoefficient__c,GPO__c,'+

        'AmountPurchased__c,AmountPurchased2__c,AmountPurchased3__c,AmountPurchased4__c,AmountPurchased5__c,AmountPurchased6__c,'+
        'TaskIndicatorRebate1__c,TaskIndicatorRebate2__c,TaskIndicatorRebate3__c,TaskIndicatorRebate4__c,TaskIndicatorRebate5__c,TaskIndicatorRebate6__c,'+
        'LeasingPolicyTerms__c, RentalFee__c,MaintenanceCost__c,ReturnServiceFee__c,ReturnServiceFeeRentalFee__c, SalePrice__c, Category2__c, '+
        'Product__c ,Product__r.Name , Product__r.Brand__c,Product__r.Spec__c ,WhetherIncludedTax__c, Product__r.Model__c,Product__r.NCCode__c,Product__r.Category1__c ,MarketingPolicy__c '+
        'from  MarketingPolicy__c where name != null and Active__c != true   ';
        
        String ptsql = sql ; 
        sql += ' and MarketingPolicy__c = null ';
        sql += ' and  Product__c in (\'' +String.join( pLi, '\',\'')+'\') ';

        if(!isGPO)sql += systemset == null ? '  and Company__c = \''+ip.Company__c+'\'  and System__c = null  ' : '  and System__c = \''+systemset+'\' ';
        //如果是gpo 
        if(isGPO) sql += ' and GPO__c != null ';
        Set<String > mset = new Set<String>();
        Set<String> mpset = new Set<String>();
        System.debug('InstrumentPolicyController==>'+sql);
        Set<String> policySet = new Set<String>();
        for (MarketingPolicy__c mp : Database.query(sql)) {
            
            //如果 是gpo 医院gpo == 政策gpo 
            //if(isGPO && proGPOMap.isEmpty()) continue;

          //  if( isGPO && (proGPOMap.isEmpty() || !proGPOMap.get(mp.Product__c).contains(mp.GPO__c))  )continue;
            
            
            policySet.add(mp.Id);
            if(pbMap.containsKey(mp.Product__c)){
                mset.add(mp.Product__c );
                mpset.add(mp.Id );
                for(PolicyBean pb : pbMap.get(mp.Product__c)){
                   
                    pb.pbMap.put(mp.PolicyType__c , new PolicyBean(mp, systemset) ); 

                }
                
            }

        }
       

        ptsql += ' and MarketingPolicy__c   in (\'' +String.join(new List<String>(mpset), '\',\'')+'\') ';
        //配套政策
        System.debug('InstrumentPolicyController==>ptsql'+ptsql);
        for (MarketingPolicy__c mp : Database.query(ptsql)) {
            //tmp1.add(new PolicyBean(mp,systemset));


            for(String k : pbMap.keySet()){

               for(PolicyBean pb : pbMap.get(k)){
                    for(String pk : pb.pbMap.keySet()){
                        PolicyBean p =  pb.pbMap.get(pk) ;
                        if(p.mpId == mp.MarketingPolicy__c ){
                            p.ptList.add(new PolicyBean(mp,systemset ));
                        }
                    }
               }

            }

        }

        Set<String> psets  = new Set<String>();
         for(String k : pbMap.keySet()){

            if(!mset.contains(k)){
                pbMap.remove(k);
            } else{
                psets.add(k);
                tmp.addAll(pbMap.get(k));
            }

        }
        //配套软件/设备
        Map<String, List<PolicyBean>> relatedMap =getRelatedProudctMap(psets,policySet);// new Map<String, List<PolicyBean>> ();
        /*
       for(ConfigurationListDetail__c p : [select InstrumentModel__c, Configuration__c, Id ,Configuration__r.Name ,Configuration__r.Category2__c ,Configuration__r.Spec__c,Configuration__r.NCCode__c,
        Configuration__r.Model__c ,Configuration__r.Category1__c,Configuration__r.ProductName__c, Configuration__r.Brand__c  
        from ConfigurationListDetail__c where SoftWareKit__c = true and  InstrumentModel__c in: psets]){
            
            List<PolicyBean> rtmp =  relatedMap.containsKey(p.InstrumentModel__c ) ? relatedMap.get(p.InstrumentModel__c) :  new List<PolicyBean>();

            rtmp.add(new PolicyBean(p,true));
            relatedMap.put(p.InstrumentModel__c, rtmp);


       }
      

       ////配套政策的仪器
      // Map<String ,List<PolicyBean>> policyRelatedMap = new Map<String,List<PolicyBean>>();
       for(MarketingPolicySupport__c mps : [select Number__c, MarketingPolicy__c,Id,SupportProduct__c,SupportProduct__r.Name ,SupportProduct__r.Category2__c,SupportProduct__r.Brand__c , 
        SupportProduct__r.Spec__c,SupportProduct__r.NCCode__c,SupportProduct__r.Model__c,SupportProduct__r.Category1__c,SupportProduct__r.ProductName__c,Note__c,FreeGift__c
       
        from MarketingPolicySupport__c where MarketingPolicy__c in:policySet ]){
            
            List<PolicyBean> rtmp =  relatedMap.containsKey(mps.MarketingPolicy__c ) ? relatedMap.get(mps.MarketingPolicy__c) :  new List<PolicyBean>();
            rtmp.add(new PolicyBean(mps,true));
            relatedMap.put(mps.MarketingPolicy__c, rtmp);

       }
*/
        //ptdataJson = EncodingUtil.urlEncode(JSON.serialize(tmp1), 'UTF-8');
       Map<String,Object> dM  = new Map<String,Object>();
       dM.put('policy', tmp);
        dM.put('related', relatedMap );
        System.debug('InstrumentPolicyController==>'+dM);
        System.debug(JSON.serialize(dM));
        dataJson = EncodingUtil.urlEncode(JSON.serialize(dM), 'UTF-8');


/*


        //


        /////////////////////


        String sql = ' select Id ,PolicyType__c, EarnestMoney__c , CooperationPeriod__c , AmountPurchased__c , BloodCardPurchased__c,Dealer__c, '+
        'LeasingPolicyTerms__c, RentalFee__c,MaintenanceCost__c,ReturnServiceFee__c,ReturnServiceFeeRentalFee__c, SalePrice__c, Category2__c, '+
        'Product__c ,Product__r.Name , Product__r.Brand__c,Product__r.Spec__c , Product__r.Model__c,Product__r.NCCode__c,Product__r.Category1__c ,MarketingPolicy__c '+
        'from  MarketingPolicy__c where name != null    ';

        String ptsql = sql ; 
        sql += ' and MarketingPolicy__c = null ';
        if(!isDealer){
            List<String> pL = new List<String>(); 
            for(IntentProduct__c p :[select Product__c from  IntentProduct__c where Opportunity__c =:ip.Opportunity__c and Product__r.Category0__c = '仪器']){


                pl.add(p.Product__c);
            }
            if(pl.size() == 0 ) return;
           sql += ' and  Product__c in (\'' +String.join(pl, '\',\'')+'\') ';
        }


        sql += systemset == null ? '  and Company__c = \''+ip.Company__c+'\'  and System__c = null  ' : '  and System__c = \''+systemset+'\' ';
        
        if(isDealer){

            System.debug('InstrumentPolicyController==>'+searchJson);
            Map<String,Object> searchMap = ( Map<String,Object>)JSON.deserializeUntyped(EncodingUtil.urlDecode(searchJson, 'UTF-8')); 

            if(String.isBlank(String.valueOf(searchMap.get('name'))) == false)sql += ' and Product__r.ProductName__c like \'%'+String.valueOf(searchMap.get('name'))+'%\'';
          
            if(String.isBlank(String.valueOf(searchMap.get('brand'))) == false)sql += ' and Product__r.Brand__c like \'%'+String.valueOf(searchMap.get('brand'))+'%\'';
          
            if(String.isBlank(String.valueOf(searchMap.get('serialno'))) == false)sql += ' and Product__r.SerialNo__c like \'%'+String.valueOf(searchMap.get('serialno'))+'%\'';
          
            if(String.isBlank(String.valueOf(searchMap.get('model'))) == false)sql += ' and Product__r.Model__c like \'%'+String.valueOf(searchMap.get('model'))+'%\'';
          
            if(String.isBlank(String.valueOf(searchMap.get('nccode'))) == false)sql += ' and Product__r.NCCode__c like \'%'+String.valueOf(searchMap.get('nccode'))+'%\'';
          
            if(String.isBlank(String.valueOf(searchMap.get('cate1'))) == false)sql += ' and Product__r.Category1__c like \'%'+String.valueOf(searchMap.get('cate1'))+'%\'';
          
            if(String.isBlank(String.valueOf(searchMap.get('cate2'))) == false)sql += ' and Product__r.Category2__c like \'%'+String.valueOf(searchMap.get('cate2'))+'%\'';
          
            if(String.isBlank(String.valueOf(searchMap.get('cate3'))) == false)sql += ' and Product__r.Category3__c like \'%'+String.valueOf(searchMap.get('cate3'))+'%\'';
               
        }


        System.debug('InstrumentPolicyController==>'+sql);
        Set<String> midset = new Set<String>();
        for (MarketingPolicy__c mp : Database.query(sql)) {
            midset.add(mp.Id);
            tmp.add(new PolicyBean(mp,systemset));
        }
        ptsql += ' and MarketingPolicy__c  in (\'' +String.join(new List<String>(midset), '\',\'')+'\') ';
        for (MarketingPolicy__c mp : Database.query(ptsql)) {
            tmp1.add(new PolicyBean(mp,systemset));
        }
        ptdataJson = EncodingUtil.urlEncode(JSON.serialize(tmp1), 'UTF-8');
        dataJson = EncodingUtil.urlEncode(JSON.serialize(tmp), 'UTF-8');
        */

    }

    //获取配套的软件/设备
    private Map<String, List<PolicyBean>>  getRelatedProudctMap(Set<String > psets ,Set<String> policySet ){

          //配套软件/设备
        Map<String, List<PolicyBean>> relatedMap = new Map<String, List<PolicyBean>> ();

       for(ConfigurationListDetail__c p : [select InstrumentModel__c, Configuration__c, Id ,Configuration__r.Name ,Configuration__r.Category2__c ,Configuration__r.Spec__c,Configuration__r.NCCode__c,
        Configuration__r.Model__c ,Configuration__r.Category1__c,Configuration__r.ProductName__c, Configuration__r.Brand__c,Amount__c  
        from ConfigurationListDetail__c where SoftWareKit__c = true and  InstrumentModel__c in: psets]){
            
            if(p.InstrumentModel__c == null) continue;
            List<PolicyBean> rtmp =  relatedMap.containsKey(p.InstrumentModel__c ) ? relatedMap.get(p.InstrumentModel__c) :  new List<PolicyBean>();

            rtmp.add(new PolicyBean(p,true));
            relatedMap.put(p.InstrumentModel__c, rtmp);


       }
      

       ////配套政策的仪器
      // Map<String ,List<PolicyBean>> policyRelatedMap = new Map<String,List<PolicyBean>>();
       for(MarketingPolicySupport__c mps : [select Number__c, MarketingPolicy__c,Id,SupportProduct__c,SupportProduct__r.Name ,SupportProduct__r.Category2__c,SupportProduct__r.Brand__c , 
        SupportProduct__r.Spec__c,SupportProduct__r.NCCode__c,SupportProduct__r.Model__c,SupportProduct__r.Category1__c,SupportProduct__r.ProductName__c,Note__c,FreeGift__c,Type__c
       
        from MarketingPolicySupport__c where MarketingPolicy__c in:policySet ]){
            if(mps.MarketingPolicy__c == null) continue;
            List<PolicyBean> rtmp =  relatedMap.containsKey(mps.MarketingPolicy__c ) ? relatedMap.get(mps.MarketingPolicy__c) :  new List<PolicyBean>();
            rtmp.add(new PolicyBean(mps,true));
            relatedMap.put(mps.MarketingPolicy__c, rtmp);

       }
       return relatedMap;

    }


    public class PolicyBean{

        public String productName {get; set;}//仪器
        public String mpId {get; set;}//政策
        public Decimal entrueMoney {get; set;} //保证金
        public Integer coopYear  {get; set;}//合作年限
        public String startDate {get; set;}//开始日期
        public String endDate {get; set;}//结束日期
        public Decimal amountPerYear0 {get; set;}//年基本采购额
        public Decimal amountPerYear1 {get; set;}//年基本采购额
        public Decimal amountPerYear2 {get; set;}//年基本采购额
        public Decimal amountPerYear3 {get; set;}//年基本采购额
        public Decimal amountPerYear4 {get; set;}//年基本采购额
        public Decimal amountPerYear5 {get; set;}//年基本采购额

        public Decimal taskIndicator0 ;//任务指标返利计算底线
        public Decimal taskIndicator1 ;//任务指标返利计算底线
        public Decimal taskIndicator2 ;//任务指标返利计算底线
        public Decimal taskIndicator3 ;//任务指标返利计算底线
        public Decimal taskIndicator4 ;//任务指标返利计算底线
        public Decimal taskIndicator5 ;//任务指标返利计算底线

        public Decimal s_taskIndicator0 ;//任务指标返利计算底线
        public Decimal s_taskIndicator1 ;//任务指标返利计算底线
        public Decimal s_taskIndicator2 ;//任务指标返利计算底线
        public Decimal s_taskIndicator3 ;//任务指标返利计算底线
        public Decimal s_taskIndicator4 ;//任务指标返利计算底线
        public Decimal s_taskIndicator5 ;//任务指标返利计算底线


        public Integer eqNum {get; set;}//仪器数量
        public Decimal rentFee{get; set;}//租赁费
        public Decimal maintenanceFee{get; set;}//维修费
        public String brand {get; set;}//品牌
        public String cate1{get; set;}//一级分类
        public String cate2 {get; set;}//二级分类
        public String thirdsys ;//第三方

        public String spec {get; set;}//
        public String nccode {get; set;} //
        public String model {get; set;}


        public Decimal returnServiceFeeStand {get; set;} //返服务费标准
        public Decimal returnServiceAndRentFeeStand {get; set;}//返服务费,租赁费标准
        public Decimal salesprice {get; set;} //单台售价

        public Decimal subprofit {get; set;}//子公司毛利
        public Decimal groupprofit {get; set;}//集团公司毛利

        public Decimal s_entrueMoney {get; set;}//保证金

        public Decimal s_amountPerYear0 {get; set;}//年基本采购额
        public Decimal s_amountPerYear1 {get; set;}//年基本采购额
        public Decimal s_amountPerYear2 {get; set;}//年基本采购额
        public Decimal s_amountPerYear3 {get; set;}//年基本采购额
        public Decimal s_amountPerYear4 {get; set;}//年基本采购额
        public Decimal s_amountPerYear5 {get; set;}//年基本采购额


        public Integer s_coopYear  {get; set;}//合作年限
        public Decimal s_rentFee {get; set;} //租赁费
        public Decimal s_maintenanceFee {get; set;}//维修费
        public Decimal s_returnServiceFeeStand {get; set;} //返服务费标准
        public Decimal s_returnServiceAndRentFeeStand {get; set;}//返服务费,租赁费标准
        public Decimal s_salesprice {get; set;} //单台售价

        public String policyItems {get;set;}//政策条款
        public String s_policyItems {get;set;}//模版政策条款

        public  Decimal supportAmount ; //投放支持金额;
        public Decimal bloodNo  ; //血型卡数
        public Decimal s_bloodNo ; //模版血型卡数
        public Decimal bloodPrice  ; //血型单价

        public Map<String,PolicyBean> pbMap ; //该仪器拥有的政策

        public List<PolicyBean> ptList ;//该政策的配套政策
        public String terimalId ; //终端Id ; 
        public String terimal;//终端名称

        public Boolean isfree ;//是否是赠送仪器

        public String type ;

        public String tax;//是否含税
        public String s_tax;//是否含税

        public String productId; 
        public String specificItems;
        public String impId ;
        public String dealer ; 
        public boolean isEidtable ; 
        public String ptId ; 
        public Boolean isRelated;//是否配套
        public String relatedType ;//配套类型  : 系统自动带出 , 需要自行选择  
        public String relatedPolicyId; 
        public Boolean isgift;//是否赠送 : 赠送的配套仪器 默认为销售出库

        public String note ; //配套仪器备注

        //增加了3个字段 

        public Decimal presentRegent;//增送试剂金额
        public Decimal s_presentRegent;//增送试剂金额
        //public Decimal TaskIndicatorRebate1;//第一年返利底线
        public Decimal rewardCoefficient; //奖励系数
        public Decimal s_rewardCoefficient; //奖励系数

        public PolicyBean(InstrumentMarketingPolicy__c imp ,String  thir ){
            presentRegent = imp.PresentingReagent__c;
            s_presentRegent  = imp.MarketingPolicy__r.PresentingReagent__c;
            rewardCoefficient = imp.RewardCoefficient__c ; 
            s_rewardCoefficient = imp.MarketingPolicy__r.RewardCoefficient__c;

            isRelated = imp.IsRelated__c;
            isEidtable = true; //imp.MarketingPolicy__r.MarketingPolicy__c == null ;
            ptId = imp.MarketingPolicy__r.MarketingPolicy__c;
            impId = imp.Id ; 
            productId = imp.Product__c; 
            thirdsys = thir ;
            productName = (imp.IsRelated__c == true || imp.IsFree__c == true ) ? imp.Product__r.Name: imp.MarketingPolicy__r.Product__r.Name  ;
            mpId = imp.MarketingPolicy__c;
            specificItems =imp.SpecificItems__c == null  ?'' : imp.SpecificItems__c ;

            entrueMoney = imp.EarnestMoney__c;
            coopYear = imp.CooperationPeriod__c == null ? 0 : Integer.valueOf(imp.CooperationPeriod__c);
            startDate = imp.StartDate__c == null ? '': imp.StartDate__c.format();
            endDate = imp.EndDate__c == null ? '' : imp.EndDate__c.format();

            amountPerYear0 = imp.AmountPurchased__c ;
            amountPerYear1 = imp.AmountPurchased2__c ;
            amountPerYear2 = imp.AmountPurchased3__c ;
            amountPerYear3 = imp.AmountPurchased4__c ;
            amountPerYear4 = imp.AmountPurchased5__c ;
            amountPerYear5 = imp.AmountPurchased6__c ;

            taskIndicator0 = imp.TaskIndicatorRebate1__c;
            taskIndicator1 = imp.TaskIndicatorRebate2__c;
            taskIndicator2 = imp.TaskIndicatorRebate3__c;
            taskIndicator3 = imp.TaskIndicatorRebate4__c;
            taskIndicator4 = imp.TaskIndicatorRebate5__c;
            taskIndicator5 = imp.TaskIndicatorRebate6__c;

            s_taskIndicator0 = imp.MarketingPolicy__r.TaskIndicatorRebate1__c;
            s_taskIndicator1 = imp.MarketingPolicy__r.TaskIndicatorRebate2__c;
            s_taskIndicator2 = imp.MarketingPolicy__r.TaskIndicatorRebate3__c;
            s_taskIndicator3 = imp.MarketingPolicy__r.TaskIndicatorRebate4__c;
            s_taskIndicator4 = imp.MarketingPolicy__r.TaskIndicatorRebate5__c;
            s_taskIndicator5 = imp.MarketingPolicy__r.TaskIndicatorRebate6__c;

            tax = imp.MarketingPolicy__r.WhetherIncludedTax__c;
            s_tax = imp.MarketingPolicy__r.WhetherIncludedTax__c;


            terimal = imp.TerminalAccunt__r.Name ;

            eqNum = imp.Quantity__c == null ? null : Integer.valueOf(imp.Quantity__c);
            rentFee = imp.RentalFee__c;
            maintenanceFee = imp.MaintenanceCost__c;

            brand = imp.ProductBrand__c;
            cate1 = imp.ProductLine__c;
            cate2 = imp.Category2__c;
            type = imp.PolicyType__c;
            spec = imp.Product__r.Spec__c;
            nccode = imp.Product__r.NCCode__c;
          //  serialno = imp.MarketingPolicy__r.Product__r.SerialNo__c; 
            model = imp.MarketingPolicy__r.Product__r.Model__c;
            supportAmount = imp.SupportAmount__c == null ? 0:imp.SupportAmount__c  ; 

            returnServiceFeeStand = imp.ReturnServiceFee__c == null ? 0 : imp.ReturnServiceFee__c ;
            returnServiceAndRentFeeStand = imp.ReturnServiceFeeRentalFee__c == null ? 0 :imp.ReturnServiceFeeRentalFee__c;
            salesprice = imp.SalePrice__c;
            dealer = imp.MarketingPolicy__r.Dealer__c;

            policyItems = imp.LeasingPolicyTerms__c;


            s_entrueMoney = imp.MarketingPolicy__r.EarnestMoney__c;
            s_coopYear = imp.MarketingPolicy__r.CooperationPeriod__c == null ? 0 : Integer.valueOf(imp.MarketingPolicy__r.CooperationPeriod__c);

            s_amountPerYear0 = imp.MarketingPolicy__r.AmountPurchased__c ;
            s_amountPerYear1 = imp.MarketingPolicy__r.AmountPurchased2__c ;
            s_amountPerYear2 = imp.MarketingPolicy__r.AmountPurchased3__c ;
            s_amountPerYear3 = imp.MarketingPolicy__r.AmountPurchased4__c ;
            s_amountPerYear4 = imp.MarketingPolicy__r.AmountPurchased5__c ;
            s_amountPerYear5 = imp.MarketingPolicy__r.AmountPurchased6__c ;



            s_rentFee = imp.MarketingPolicy__r.RentalFee__c;
            s_maintenanceFee = imp.MarketingPolicy__r.MaintenanceCost__c;
            s_salesprice = imp.MarketingPolicy__r.SalePrice__c;

            s_returnServiceFeeStand = imp.MarketingPolicy__r.ReturnServiceFee__c;
            s_returnServiceAndRentFeeStand = imp.MarketingPolicy__r.ReturnServiceFeeRentalFee__c;
            s_policyItems = imp.MarketingPolicy__r.LeasingPolicyTerms__c;
            s_bloodNo = imp.MarketingPolicy__r.BloodCardPurchased__c ;
            bloodNo = imp.BloodCardNumber__c; 
            bloodPrice = imp.BloodCardPrice__c == null  ? 0 :imp.BloodCardPrice__c ; 
            isfree = imp.IsFree__c;
            ptList = new List<PolicyBean>();

            isgift = imp.FreeGift__c;

            note = imp.Note__c;
            terimalId = imp.TerminalAccunt__c;
            terimal = imp.TerminalAccunt__r.Name ;

            relatedType = imp.RelatedType__c;

        }

        public PolicyBean(ConfigurationListDetail__c p,Boolean isRe ){
            productId = p.Configuration__c ; 
            eqNum = Integer.valueOf(p.Amount__c)  ;
            cate2 = p.Configuration__r.Category2__c;
            spec = p.Configuration__r.Spec__c;
            nccode = p.Configuration__r.NCCode__c;
            model = p.Configuration__r.Model__c;
            cate1 = p.Configuration__r.Category1__c;
            productName = p.Configuration__r.Name ;
            brand = p.Configuration__r.Brand__c;
            isRelated = isRe;
            isfree = true;
            if(isRe == true) isfree = false;
            isgift = false;
            ptList = new List<PolicyBean>();
        }
         public PolicyBean(Product__c  p,Boolean isRe ){
            productId = p.Id ; 
            eqNum = 1;
            cate2 = p.Category2__c;
            spec = p.Spec__c;
            nccode = p.NCCode__c;
            model = p.Model__c;
            cate1 = p.Category1__c;
            productName = p.Name ;
            brand = p.Brand__c;
            isRelated = isRe;
            isfree = true;
            if(isRe == true) isfree = false;
            isgift = false;
            ptList = new List<PolicyBean>();
        }

         public PolicyBean(MarketingPolicySupport__c p,Boolean isRe ){
            productId = p.SupportProduct__c  ; 
            isgift = p.FreeGift__c;
            relatedPolicyId = p.MarketingPolicy__c;

            eqNum = p.Number__c == null ? 0 :  Integer.valueOf(p.Number__c);
            cate2 = p.SupportProduct__r.Category2__c;
            spec = p.SupportProduct__r.Spec__c;
            nccode = p.SupportProduct__r.NCCode__c;
            model = p.SupportProduct__r.Model__c;
            cate1 = p.SupportProduct__r.Category1__c;
            productName = p.SupportProduct__r.Name ;
            brand = p.SupportProduct__r.Brand__c;
            relatedType = p.Type__c;

            isRelated = isRe;
            isfree = true;
            if(isRe == true) isfree = false;
              note = p.Note__c;
            ptList = new List<PolicyBean>();
        }

        public PolicyBean(IntentProduct__c ip, String thir ){

            thirdsys = thir ;
            productId = ip.Product__c; 
            eqNum = (Integer) ip.Quantity__c ;
            cate2 = ip.Product__r.Category2__c;
            spec = ip.Product__r.Spec__c;
            nccode = ip.Product__r.NCCode__c;
            model = ip.Product__r.Model__c;
            cate1 = ip.Product__r.Category1__c;
            productName = ip.Product__r.Name ;
            pbMap = new Map<String,PolicyBean >();
            brand = ip.Product__r.Brand__c;

            terimal = ip.TerminalClient__r.Name;
            terimalId = ip.TerminalClient__c;
            isfree = false;
            ptList = new List<PolicyBean>();
            isgift = false;


        }

        public PolicyBean(MarketingPolicy__c imp ,String  thir ){

            presentRegent = imp.PresentingReagent__c;
            s_presentRegent  = imp.PresentingReagent__c;
            rewardCoefficient = imp.RewardCoefficient__c ; 
            s_rewardCoefficient = imp.RewardCoefficient__c;

            isEidtable = true; //imp.MarketingPolicy__c == null ;
            ptId = imp.MarketingPolicy__c;
            thirdsys = thir ;
            productId = imp.Product__c; 
            productName = imp.Product__r.Name  ;
            mpId = imp.Id ;
            specificItems = '';
            entrueMoney = imp.EarnestMoney__c;
            coopYear = imp.CooperationPeriod__c == null ? 0 : Integer.valueOf(imp.CooperationPeriod__c);
            
            isgift = false;
            amountPerYear0 = imp.AmountPurchased__c ;
            amountPerYear1 = imp.AmountPurchased2__c ;
            amountPerYear2 = imp.AmountPurchased3__c ;
            amountPerYear3 = imp.AmountPurchased4__c ;
            amountPerYear4 = imp.AmountPurchased5__c ;
            amountPerYear5 = imp.AmountPurchased6__c ;


            taskIndicator0 = imp.TaskIndicatorRebate1__c;
            taskIndicator1 = imp.TaskIndicatorRebate2__c;
            taskIndicator2 = imp.TaskIndicatorRebate3__c;
            taskIndicator3 = imp.TaskIndicatorRebate4__c;
            taskIndicator4 = imp.TaskIndicatorRebate5__c;
            taskIndicator5 = imp.TaskIndicatorRebate6__c;

            s_taskIndicator0 = imp.TaskIndicatorRebate1__c;
            s_taskIndicator1 = imp.TaskIndicatorRebate2__c;
            s_taskIndicator2 = imp.TaskIndicatorRebate3__c;
            s_taskIndicator3 = imp.TaskIndicatorRebate4__c;
            s_taskIndicator4 = imp.TaskIndicatorRebate5__c;
            s_taskIndicator5 = imp.TaskIndicatorRebate6__c;
            tax = imp.WhetherIncludedTax__c;
            s_tax = imp.WhetherIncludedTax__c;



            rentFee = imp.RentalFee__c;
            maintenanceFee = imp.MaintenanceCost__c;

            brand = imp.Product__r.Brand__c ;
            
            type = imp.PolicyType__c ;
            supportAmount = 0 ; 

            returnServiceFeeStand = imp.ReturnServiceFee__c == null ? 0 :imp.ReturnServiceFee__c ;
            returnServiceAndRentFeeStand = imp.ReturnServiceFeeRentalFee__c == null ? 0 : imp.ReturnServiceFeeRentalFee__c;
            salesprice = imp.SalePrice__c;

            eqNum = 0;
            cate2 = imp.Category2__c;

            s_entrueMoney = imp.EarnestMoney__c;
            s_coopYear = imp.CooperationPeriod__c == null ? 0 : Integer.valueOf(imp.CooperationPeriod__c);

            s_amountPerYear0 = imp.AmountPurchased__c ;
            s_amountPerYear1 = imp.AmountPurchased2__c ;
            s_amountPerYear2 = imp.AmountPurchased3__c ;
            s_amountPerYear3 = imp.AmountPurchased4__c ;
            s_amountPerYear4 = imp.AmountPurchased5__c ;
            s_amountPerYear5 = imp.AmountPurchased6__c ;


            s_rentFee = imp.RentalFee__c;
            s_maintenanceFee = imp.MaintenanceCost__c;
            s_bloodNo = imp.BloodCardPurchased__c  == null ? 0 : imp.BloodCardPurchased__c ;
            bloodNo = s_bloodNo; 
            bloodPrice = 0 ; 
            dealer = imp.Dealer__c;

            cate1 = imp.Product__r.Category1__c;

            s_returnServiceFeeStand = imp.ReturnServiceFee__c;
            s_returnServiceAndRentFeeStand = imp.ReturnServiceFeeRentalFee__c;
            s_salesprice = imp.SalePrice__c;
            s_policyItems = imp.LeasingPolicyTerms__c;

            spec = imp.Product__r.Spec__c;
            nccode = imp.Product__r.NCCode__c;
            model = imp.Product__r.Model__c;
            isfree = false;

            ptList = new List<PolicyBean>();


           // serialno = imp.Product__r.SerialNo__c;

        }



    }


}